// pages/getcoupon/index.js
const { Http } = require("../../utils/http")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    receiveName: "",
    telephone: "",
    coupon_id: "",
    shop_name: "请选择店铺( 必选项 )",
    store_id: ""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log(options);
    this.setData({
      store_id: options.store_id,
      shop_name: options.store_name,
      coupon_id: options.coupon_id,
      receiveName:options.username,
      telephone:options.phone
    })
  },
  orderPay: function () {
    // console.log(this.data.receiveName);
    if (this.data.store_id == null || this.data.store_id == "") {
      wx.showModal({
        title: '提示',
        content: "选择门店"
      })
    } else if (!(/^[\u4E00-\u9FA5A-Za-z]+$/.test(this.data.receiveName))) {
      wx.showToast({
        title: '请认真填写姓名',
        duration: 2000,
        icon: 'none'
      });
    } else if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(this.data.telephone))) {
      wx.showToast({
        title: '请认真填写手机号码',
        duration: 2000,
        icon: 'none'
      });
    } else {
      this.subt(this.data.receiveName, this.data.telephone);
    }
  },
  subt: async function (receiveName, telephone) {
    console.log(this.data.coupon_id);
    let result = await Http.request({
      url: 'wechat/coupon/get_free_coupon',
      data: { coupon_id: wx.getStorageSync('sceneCs'), username: receiveName, mobile: telephone, store_id: this.data.store_id },
      method: "POST"
    });
    console.log(result);
    if (result.code == 0) {
      wx.showModal({
        title: '提示',
        content: result.msg + ",是否进入领取中心查看",
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/ordercenter/index',
            })
            console.log('用户点击确定')
          } else if (res.cancel) {
            wx.navigateTo({
              url: '/pages/run/index',
            })
            console.log('用户点击取消')
          }
        }
      });

    } else {
      console.log(result);
      wx.showToast({
        title: result.msg,
        duration: 2000,
        icon: 'none'
      });
    }
  }
})